/*
 * AccountManager.java
 *
 * Distributed Systems: Project 2
 *
 */

package unimelb.distSystems.communications;

import unimelb.distSystems.communications.CORBA.*;

import java.util.*;


public class RoomsManager {
    private List rooms;
    

    public RoomsManager(){
        rooms = new ArrayList();
    }

    public MatchRoom addNewMatchRoom(ServiceMatchRoom room){
        MatchRoom newRoom = new MatchRoom(room);
        if(!exists(newRoom.getName())){
            newRoom.generateID();
            while(!validateID(newRoom.getMatchroom_id())){
                newRoom.adjustID();
            }
            rooms.add(newRoom);
        } else {
            return null;
        }
        return newRoom;
    }

    // Validates if a matchroom already exists
    public boolean exists(String roomName){
        ListIterator li = rooms.listIterator();

        while(li.hasNext()){
            MatchRoom room = (MatchRoom)li.next();
            if(room.getName().equals(roomName)){
                return true;
            }
        }
        return false;
    }

    // Validates if a rooms's id already exists
    public boolean validateID(long id){
        ListIterator li = rooms.listIterator();

        while(li.hasNext()){
            MatchRoom room = (MatchRoom)li.next();
            if(room.getMatchroom_id() == id){
                return false;
            }
        }
        return true;
    }

    public MatchRoom getMatchRoom(long id){
        ListIterator li = rooms.listIterator();

        while(li.hasNext()){
            MatchRoom room = (MatchRoom)li.next();
            if(room.getMatchroom_id() == id){
                return room;
            }
        }
        return null;
    }

    public ServiceMatchRoom getServiceRoom(long id){
        ListIterator li = rooms.listIterator();

        while(li.hasNext()){
            MatchRoom room = (MatchRoom)li.next();
            if(room.getMatchroom_id() == id){
                return room.getServiceMatchRoom();
            }
        }
        return null;
    }

    public ServiceMatchRoom[] getRooms(){
        ListIterator li = rooms.listIterator();
        ServiceMatchRoom[] list = new ServiceMatchRoom[rooms.size()];
        int index = 0;

        while(li.hasNext()){
            index = li.nextIndex();
            MatchRoom room = (MatchRoom)li.next();
            list[index] = room.getServiceMatchRoom();
        }

        return list;
    }

    public ServiceMatchRoom[] searchRoom(String word){
        List list = new ArrayList();
        ListIterator li = rooms.listIterator();
        MatchRoom room;
        ServiceMatchRoom sRoom;

        if(word.length() > 0){
            while(li.hasNext()){
                room = (MatchRoom)li.next();
                if(room.getName().contains(word.subSequence(0, word.length()))){
                    list.add(room.getServiceMatchRoom());
                }
            }
        }

        ServiceMatchRoom[] result = new ServiceMatchRoom[list.size()];
        int index = 0;
        li = list.listIterator();
        while(li.hasNext()){
            index = li.nextIndex();
            sRoom = (ServiceMatchRoom)li.next();
            result[index] = sRoom;
        }

        return result;
    }
    
    public void deleteRoom(long room_id)
    {
    	for(int i=0;i<rooms.size();i++)
    	{
    		MatchRoom this_room = (MatchRoom) rooms.get(i);
    		if(this_room.getMatchroom_id()==room_id)
    			rooms.remove(i);
    	}
    }

    public void replicateChat(long roomID){
        MatchRoom room = getMatchRoom(roomID);
        ListIterator li = room.getPlayers().listIterator();

        while(li.hasNext()){
            Player player = (Player)li.next();
            UserAccount a = player.getAcount();
            a.getCallback().updateChat(room.getChatArray());
        }
    }

    public void replicatePlayers(long roomID){
        MatchRoom room = getMatchRoom(roomID);
        ListIterator li = room.getPlayers().listIterator();

        while(li.hasNext()){
            Player player = (Player)li.next();
            UserAccount a = player.getAcount();
            a.getCallback().updatePlayers(room.getPlayersArray());
        }
    }

    public void signalRaceStart(long roomID){
        MatchRoom room = getMatchRoom(roomID);
        room.setRaceInProgress(true);
        ListIterator li = room.getPlayers().listIterator();

        while(li.hasNext()){
            Player player = (Player)li.next();
            UserAccount a = player.getAcount();
            a.getCallback().marks(room.getPlayersArray());
        }
    }

    public void signalRaceGo(long roomID){
        MatchRoom room = getMatchRoom(roomID);
        ListIterator li = room.getPlayers().listIterator();

        while(li.hasNext()){
            Player player = (Player)li.next();
            UserAccount a = player.getAcount();
            a.getCallback().go();
        }
    }

    public void replicateAction(long roomID, ServiceAction action){
        MatchRoom room = getMatchRoom(roomID);
        ListIterator li = room.getPlayers().listIterator();

        while(li.hasNext()){
            Player player = (Player)li.next();
            UserAccount a = player.getAcount();
            if(action.playerID != a.getUserID()){
                a.getCallback().takeAction(action);
            }
        }
    }

    public void callOnLoosers(long roomID, long winnerID){
        MatchRoom room = getMatchRoom(roomID);
        ListIterator li = room.getPlayers().listIterator();

        while(li.hasNext()){
            Player player = (Player)li.next();
            UserAccount a = player.getAcount();
            if(winnerID != a.getUserID()){
                a.getCallback().endGame();
            }
        }
    }

}
